package cn.hfuu.flink;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @author oldliu
 * @since 1.0
 */
public class WordCount {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        //DataSource<String> xDataSource = env.readTextFile("E:\\hadoop-2.7.3\\LICENSE.txt");
        DataSource<String> xDataSource = env.readTextFile("file:///usr/local/customers.csv");

//        FlatMapOperator<String, Tuple2<String,Long>> result = xDataSource.flatMap((String x,
//        Collector<Tuple2<String, Long>> y) -> {
//            String[] split = x.split("(\\.|,|\\s+)");
//            for (String word : split) {
//                y.collect(Tuple2.of(word, 1L));
//            }
//        });
//        xDataSource.flatMap((x, y) -> {
//            String[] split = x.split("(\"|\\.|,|\\s+|\")");
//            for (String word : split) {
//                y.collect(Tuple2.of(word, 1L));
//            }
//        });
        FlatMapOperator<String, Tuple2<String, Long>> result = xDataSource.flatMap(new FlatMapFunction<String,
                Tuple2<String, Long>>() {

            @Override
            public void flatMap(String x, Collector<Tuple2<String, Long>> y) throws Exception {
                String[] split = x.split("(\"|\\.|,|\\s+|\")");
                for (String word : split) {
                    y.collect(Tuple2.of(word, 1L));
                }
            }
        });
        AggregateOperator<Tuple2<String, Long>> sum = result.groupBy(0).sum(1);

        sum.print();

    }
}
